package com.neusoft.ysu.npms.controller;


import com.neusoft.ysu.npms.pojo.*;
import com.neusoft.ysu.npms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者：孙加倍
 * @since 2024-06-18
 */
@RestController
@RequestMapping("/applypoor")
public class ApplypoorController {
    @Autowired
    IApplypoorService applypoorService;
    @Autowired
    IFarmerService farmerService;
    @Autowired
    IRiskService riskService;
    @Autowired
    ISecondriskService secondriskService;

    @RequestMapping("/add")
    public Result add(@RequestBody ApplypoorPlus applypoorPlus) {
        int score =0;
        score+=secondriskService.findByName(applypoorPlus.getRisk1());
        score+=secondriskService.findByName(applypoorPlus.getRisk2());
        score+=secondriskService.findByName(applypoorPlus.getRisk3());
        score+=secondriskService.findByName(applypoorPlus.getRisk4());
        score+=secondriskService.findByName(applypoorPlus.getRisk5());
        score+=secondriskService.findByName(applypoorPlus.getRisk6());
        System.out.println(score);
        System.out.println(11111111);
        Farmer farmer = new Farmer();
        farmer.setName(applypoorPlus.getName());
        farmer.setIdcard(applypoorPlus.getIdcard());
        if(applypoorPlus.getPoor()=="0")
        {
            farmer.setPoor(false);

        }else{
            farmer.setPoor(true);
        }
        if(score<=3){
            farmer.setPoorlevel(0);
        } else if (score<=7) {
            farmer.setPoorlevel(1);
        } else if (score<=16) {
            farmer.setPoorlevel(2);
        }else {
            farmer.setPoorlevel(3);
        }
        farmer.setCity(applypoorPlus.getCityName());
        farmer.setCounty(applypoorPlus.getCountyName());
        farmer.setTown(applypoorPlus.getTownName());
        farmer.setVillage(applypoorPlus.getVillageName());
        farmerService.add(farmer);
        System.out.println(farmerService.findByName(farmer));
        Integer id = farmerService.findByName(farmer);
        Risk risk1 = new Risk();
        risk1.setFarmerid(farmerService.findByName(farmer));
        risk1.setSriskid(applypoorPlus.getRisk1());
        Risk risk2 = new Risk();
        risk2.setFarmerid(farmerService.findByName(farmer));
        risk2.setSriskid(applypoorPlus.getRisk2());
        Risk risk3 = new Risk();
        risk3.setFarmerid(farmerService.findByName(farmer));
        risk3.setSriskid(applypoorPlus.getRisk3());
        Risk risk4 = new Risk();
        risk4.setFarmerid(farmerService.findByName(farmer));
        risk4.setSriskid(applypoorPlus.getRisk4());
        Risk risk5 = new Risk();
        risk5.setFarmerid(farmerService.findByName(farmer));
        risk5.setSriskid(applypoorPlus.getRisk5());
        Risk risk6 = new Risk();
        risk6.setFarmerid(farmerService.findByName(farmer));
        risk6.setSriskid(applypoorPlus.getRisk6());
        riskService.add(risk1);
        riskService.add(risk2);
        riskService.add(risk3);
        riskService.add(risk4);
        riskService.add(risk5);
        riskService.add(risk6);
        System.out.println(farmerService.findByName(farmer));

        Applypoor applypoor = new Applypoor();
        applypoor.setApprovalid(1);
        applypoor.setFarmerid(farmerService.findByName(farmer));
        applypoor.setState(0);
        applypoor.setHelpmanid(1);
        applypoor.setMoney(0);
        applypoor.setMeasureid(1);
        applypoorService.add(applypoor);
        return Result.success();
    }
    @RequestMapping("/add2")
    public Result add2(@RequestBody HelpPlus helpPlus) {
        applypoorService.add2(helpPlus);

        return Result.success();
    }
    @RequestMapping("/add1")
    public Result add(@RequestBody Applypoor applypoor) {
        applypoorService.add(applypoor);
        return Result.success();
    }
    @RequestMapping("/delete")
    public Result deleteMeasure(@RequestBody Applypoor applypoor) {
//        System.out.println(department);
        applypoorService.deleteApplypoor(applypoor);
        return Result.success();
    }
    @RequestMapping("/update")
    public Result updateMeasure(@RequestBody Applypoor applypoor){
        applypoorService.updateApplypoor(applypoor);
        return Result.success();
    }

    @RequestMapping("getAll")
    public Result getAll(){
        applypoorService.selectAll();
        return Result.success();
    }

    @RequestMapping("/findone")
    public Result findone(@RequestBody Applypoor applypoor){
        System.out.println(applypoor);
        applypoorService.findById(applypoor);
        return Result.success();
    }
    @RequestMapping("/list")
    public Result list(@RequestBody MonitorInfo monitorInfo,
                       @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize){
        System.out.println(monitorInfo.getCityName());
        return Result.success(applypoorService.getlist(monitorInfo, pageNum, pageSize));
    }

    @RequestMapping("/searchState")
    public Result searchState(@RequestBody MonitorInfo monitorInfo){
        List<MonitorInfo> monitors=  applypoorService.searchState(monitorInfo);

        return Result.success(monitors);
    }
}
